package com.herench.bunch.emr.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.datatype.CE;
import ca.uhn.hl7v2.model.v24.group.ORU_R01_PATIENT_RESULT;
import ca.uhn.hl7v2.model.v24.group.RTB_K13_ROW_DEFINITION;
import ca.uhn.hl7v2.model.v24.message.ORU_R01;
import ca.uhn.hl7v2.model.v24.message.QBP_Q11;
import ca.uhn.hl7v2.model.v24.message.QRY_Q01;
import ca.uhn.hl7v2.model.v24.message.RTB_K13;
import ca.uhn.hl7v2.model.v24.segment.*;
import ca.uhn.hl7v2.parser.DefaultModelClassFactory;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class LisItemsConvert extends AbstractMessageConvert {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String, Object> doToMessage(RequestMessage requestMessage) throws Exception {
        Map<String, Object> params = new HashMap<>();
        QRY_Q01 hl7Message = (QRY_Q01) requestMessage.getHl7Message();
        QRD qrd = hl7Message.getQRD();
        String patientId = qrd.getQrd4_QueryID().getValue();
        String startTime = qrd.getQrd1_QueryDateTime().getTs1_TimeOfAnEvent().getValue();
        String endTime = qrd.getQrd2_QueryFormatCode().getValue();
        params.put("patientId", patientId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return params;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        List<Map<String, Object>> res = (List<Map<String, Object>>) message;
        ORU_R01 oru_r01 = new ORU_R01();
        oru_r01.initQuickstart("ORU_R01", "ORU_R01", "P");
        MSH msh = oru_r01.getMSH();
        initResponseMessageMSH(msh);
        Map<String, Object> reqMap = res.get(0);

        ORU_R01_PATIENT_RESULT patient_result = oru_r01.getPATIENT_RESULT(0);

        PID pid = patient_result.getPATIENT().getPID();

        pid.getPid2_PatientID().getCx1_ID().setValue(String.valueOf(reqMap.get("patientId")));
        pid.getPid2_PatientID().getIdentifierTypeCode().setValue(String.valueOf(reqMap.get("visitId")));
        pid.getPid1_SetIDPID().setValue(String.valueOf(reqMap.get("inpNo")));

        for (int index = 0; index < res.size(); index++) {
            reqMap = res.get(index);
            OBR obr = patient_result.getORDER_OBSERVATION(index++).getOBR();

            obr.getObr1_SetIDOBR().setValue(String.valueOf(reqMap.get("labApplyNo")));
            obr.getObr15_SpecimenSource().getSps1_SpecimenSourceNameOrCode().getCe1_Identifier().setValue(String.valueOf(reqMap.get("specimenNo")));
            obr.getObr15_SpecimenSource().getSps3_Freetext().setValue(String.valueOf(reqMap.get("specimen")));
            obr.getObr15_SpecimenSource().getSps2_Additives().setValue(String.valueOf(reqMap.get("specimenCode")));
            obr.getObr2_PlacerOrderNumber().getEi1_EntityIdentifier().setValue(String.valueOf(reqMap.get("labMemo")));
            obr.getObr8_ObservationEndDateTime().getTs1_TimeOfAnEvent().setValue(String.valueOf(reqMap.get("reportDateTime")));
            obr.getObr15_SpecimenSource().getSps4_BodySite().getCe4_AlternateIdentifier().setValue(String.valueOf(reqMap.get("isValid")));
            obr.getObr15_SpecimenSource().getSps4_BodySite().getCe1_Identifier().setValue(String.valueOf(reqMap.get("microFlag")));
            obr.getObr18_PlacerField1().setValue(String.valueOf(reqMap.get("parItemNo")));
            obr.getObr19_PlacerField2().setValue(String.valueOf(reqMap.get("parItemName")));
            obr.getObr20_FillerField1().setValue(String.valueOf(reqMap.get("orderCode")));
            obr.getObr21_FillerField2().setValue(String.valueOf(reqMap.get("orderText")));
            obr.getObr11_SpecimenActionCode().setValue(String.valueOf(reqMap.get("fileVisitType")));
        }

        return oru_r01;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QBP_Q11 qbp = new QBP_Q11();
        qbp.initQuickstart("QBP_Q11", "Q11", "L");
        MSH msh = qbp.getMSH();
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMessageControlID().setValue(IdGenerator.getUUID());
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("200140");
        msh.getMsh11_ProcessingID().getProcessingMode().setValue("QR");
        QPD qpd = qbp.getQPD();
        qpd.getMessageQueryName().getIdentifier().setValue("查询号别");
        qpd.getQpd2_QueryTag().setValue("QR");
        return qbp;
    }
}
